package io.bdmc.common.utils;

import io.bdmc.common.annotation.FieldMeta;
import io.bdmc.common.utils.Constant.ModifyName;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompareObjectUtils {

    /**
     * 获取两个对象同名属性内容不相同的列表
     * 
     * @param class1 对象1
     * @param class2 对象2
     * @return
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     */
    public static List<Map<String, Object>> compareTwoClass(Object class1, Object class2) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        // 获取对象的class
        Class<?> clazz1 = class1.getClass();
        Class<?> clazz2 = class2.getClass();
        // 获取对象的属性列表
        Field[] field1 = clazz1.getDeclaredFields();
        Field[] field2 = clazz2.getDeclaredFields();
        // 遍历属性列表field1
        for (int i = 0; i < field1.length; i++) {
            if (field1[i].isAnnotationPresent(FieldMeta.class))
                // 遍历属性列表field2
                for (int j = 0; j < field2.length; j++) {
                    // 如果field1[i]属性名与field2[j]属性名内容相同
                    if (field1[i].getName().equals(field2[j].getName())) {
                        field1[i].setAccessible(true);
                        field2[j].setAccessible(true);
                        // 如果field1[i]属性值与field2[j]属性值内容不相同
                        try {
                            if (!compareTwo(field1[i].get(class1), field2[j].get(class2))
                                    && field1[i].isAnnotationPresent(FieldMeta.class)
                                    && field2[j].isAnnotationPresent(FieldMeta.class)) {
                                FieldMeta metaAnnotation = field1[i].getAnnotation(FieldMeta.class);
                                Map<String, Object> map2 = new HashMap<String, Object>();
                                map2.put("name", metaAnnotation.name());
                                map2.put("old", field1[i].get(class1) == null ? "" : field1[i].get(class1));
                                map2.put("new", field2[j].get(class2));
                                // //解决时间格式化问题-bean上加了@DateTimeFormat(pattern="yyyy-MM-dd")
                                // if(field1[i].isAnnotationPresent(DateTimeFormat.class) &&
                                // field2[j].isAnnotationPresent(DateTimeFormat.class) ){
                                // String old = DateUtils.formatDate((Date)
                                // field1[i].get(class1),field1[i].getAnnotation(DateTimeFormat.class).pattern());
                                // map2.put("old",old == null ? "": old);
                                // map2.put("new", DateUtils.formatDate((Date)
                                // field2[j].get(class2),field2[j].getAnnotation(DateTimeFormat.class).pattern()));
                                // }
                                // //解决数据字典text/value转换问题-bean上加了@Dict(dicCode = "groupField",isCommon = false)
                                // if(field1[i].isAnnotationPresent(Dictionary.class) &&
                                // field2[j].isAnnotationPresent(Dictionary.class) ){
                                // LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                                // boolean isCommon = field1[i].getAnnotation(Dict.class).isCommon();
                                // if(!isCommon){
                                // map2.put("old",compareObjectUtils.sysDictMapper.queryDictTextByKey(field1[i].getAnnotation(Dict.class).dicCode(),
                                // (String) field1[i].get(class1),null));
                                // map2.put("new",compareObjectUtils.sysDictMapper.queryDictTextByKey(field2[j].getAnnotation(Dict.class).dicCode(),
                                // (String) field2[j].get(class2),null));
                                // }else{
                                // map2.put("old",compareObjectUtils.sysDictMapper.queryDictTextByKey(field1[i].getAnnotation(Dict.class).dicCode(),
                                // (String) field1[i].get(class1),sysUser.getTenantId()));
                                // map2.put("new",compareObjectUtils.sysDictMapper.queryDictTextByKey(field2[j].getAnnotation(Dict.class).dicCode(),
                                // (String) field2[j].get(class2),sysUser.getTenantId()));
                                // }
                                // }
                                list.add(map2);
                            }
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {

                            e.printStackTrace();
                        }
                        break;
                    }
                }
        }
        return list;
    }

    public static List<Map<String, Object>> addOrDelOneClass(Object class1) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        // 获取对象的class
        Class<?> clazz1 = class1.getClass();

        // 获取对象的属性列表
        Field[] field1 = clazz1.getDeclaredFields();

        // 遍历属性列表field1
        for (int i = 0; i < field1.length; i++) {
            if (field1[i].isAnnotationPresent(FieldMeta.class)) {
                field1[i].setAccessible(true);
                FieldMeta metaAnnotation = field1[i].getAnnotation(FieldMeta.class);
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("name", metaAnnotation.name());
                try {
                    map2.put("old", field1[i].get(class1) == null ? "" : field1[i].get(class1));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                list.add(map2);
            }
        }
        return list;
    }

    // 对比两个数据是否内容相同
    public static boolean compareTwo(Object object1, Object object2) {

        if (object1 == null && object2 == null) {
            return true;
        }
        // 因源数据是没有进行赋值，是null值，改为""。
        // if (object1 == "" && object2 == null) {
        // return true;
        // }
        // if (object1 == null && object2 == "") {
        // return true;
        // }
        if (object1 == null && object2 != null) {
            return false;
        }
        if (object1.equals(object2)) {
            return true;
        }
        return false;
    }

    /**
     * 增删改日志
     * 
     * @param class1 旧数据对象 （新增或删除时 使用class1）
     * @param class2 新数据对象
     * @param type   ModifyName枚举类型 ADD,UPDATE,DELETE,GET
     * @return
     */
    public static String log(Object class1, Object class2, ModifyName type) {

        String content = "";
        if (type == ModifyName.ADD) {
            content += "增加数据：";
        } else if (type == ModifyName.DELETE) {
            content += "删除数据：";
        }
        if (type == ModifyName.UPDATE) {
            List<Map<String, Object>> list = compareTwoClass(class1, class2);
            for (Map<String, Object> map : list) {
                if (map.get("old") == "") {
                    map.put("old", "无");
                }
                content += map.get("name") + ":" + map.get("old") + " 修改为【" + map.get("new") + "】;";
            }
        } else {
            List<Map<String, Object>> list = addOrDelOneClass(class1);
            for (Map<String, Object> map : list) {
                if (map.get("old") == "") {
                    map.put("old", "无");
                }
                content += map.get("name") + ":【" + map.get("old") + "】;";
            }
        }
        return content;
    }

}
